define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trends/trends/index' + location.search,
                    add_url: 'trends/trends/add',
                    edit_url: 'trends/trends/edit',
                    del_url: 'trends/trends/del',
                    multi_url: 'trends/trends/multi',
                    import_url: 'trends/trends/import',
                    table: 'trends',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'user', title: __('User_id')},

                        {
                            field: 'image',
                            title: __('Image'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.images
                        },
                        {
                            field: 'video',
                            title: __('Video'),
                            events: Table.api.events.image,
                            operate: false,
                            formatter: function (row) {
                                if (!row) {
                                    return '-';
                                } else {
                                    var str = row.split(",");
                                    var html = '';
                                    for (var i = str.length - 1; i >= 0; i--) {
                                        html += "<a href='" + str[i] + "'target='_blank' class='fa fa-video-camera'>&nbsp;";
                                    }
                                    html += '';
                                    return html;
                                }
                            }
                        },
                        // {
                        //     field: 'content',
                        //     title: __('Content'),
                        //     width: 150,
                        //     formatter: Table.api.formatter.content,
                        //     hover: true,
                        //     operate: false
                        // },
                        {
                            field: 'like_number', title: __('Like_number'),
                            formatter: Table.api.formatter.normal,
                            icon: "fa fa-thumbs-o-up",
                            operate: false
                        },

                        {
                            field: 'comment_number', title: __('Comment_number'),
                            formatter: Table.api.formatter.normal,
                            icon: "fa fa-commenting-o",
                            operate: false
                        },
                        {
                            field: 'share_number', title: __('Share_number'),
                            formatter: Table.api.formatter.normal,
                            icon: "fa fa-share-square-o",
                            operate: false
                        },
                        {
                            field: 'collect_number', title: __('Collect_number'),
                            formatter: Table.api.formatter.normal,
                            icon: "fa fa-heart-o",
                            operate: false
                        },

                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"0": __('Status 0'), "1": __('Status 1'), "2": __('Status 2')},
                            formatter: Controller.api.formatter.status
                        },
                        {
                            field: 'remarks',
                            title: __('Remarks'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },

                        {
                            field: 'is_recommend',
                            title: __('Is_recommend'),
                            searchList: {"1": __('Is_recommend_1')},
                            formatter: Table.api.formatter.toggle
                        },

                        // {field: 'frame_status', title: __('frame_status'), searchList: {"1":__('frame_status 1'),"2":__('frame_status 2')}, formatter: Controller.api.formatter.status},
                        {
                            field: 'frame_status',
                            title: __('frame_status'),
                            searchList: {"1": __('frame_status 1'), "2": __('frame_status 2')},
                            formatter: Controller.api.formatter.custom
                        },

                        {
                            field: 'operate', title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,

                            buttons: [
                                {
                                    name: 'detail',
                                    text:__('动态操作'),
                                    title: __('动态操作'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-align-justify',
                                    url: 'trendsinfo/trendsinfo?ref=addtabs',
                                    callback: function (data, ret) {
                                        Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                    },
                                    visible:function (){
                                        return true;
                                    },
                                },

                                {
                                    name: 'trends_comment',
                                    text:__('评论记录'),
                                    title: __('评论记录'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-align-justify',
                                    url: 'trends/trends_comment/getTrendsComment?ref=addtabs',
                                    callback: function (data, ret) {
                                        Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                    },
                                    visible:function (){
                                        return true;
                                    },
                                },

                            ],


                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                status: function (value, row, index) {
                    var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
                    if (typeof this.custom !== 'undefined') {
                        custom = $.extend(custom, this.custom);
                    }
                    this.custom = custom;
                    // this.icon = 'fa fa-apple'; //更改这里图标css
                    return Table.api.formatter.normal.call(this, value, row, index);
                },
                custom: function (value, row, index) {
                    console.log(row.frame_status)
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" ' +
                        'data-url="trends/Trends/change" data-confirm="确认切换状态？" data-id="' + row.id + '">' +

                        '<i class="fa ' + (row.frame_status == '1' ? 'fa-toggle-on' : 'fa-toggle-on fa-flip-horizontal text-gray') + ' fa-2x"> </i>' +
                        '</a>';
                },
            },

        }
    };
    return Controller;
});
